package java44th.group.service.admin.auth;

import java44th.group.bean.po.*;
import java44th.group.mapper.MarketAdminMapper;
import java44th.group.mapper.MarketPermissionMapper;
import java44th.group.mapper.MarketRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName project-second
 * @Descriptiom TODO
 * @Aurhor: 蔡文强
 * @Since: 2022/10/11 21:58
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Autowired
    MarketPermissionMapper marketPermissionMapper;

    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Override
    public MarketAdmin queryAdminByUsernameAndPassword(String username,String password) {

        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);

        return marketAdmins.get(0);
    }

    @Override
    public List<String> queryPermissionByRoleId(Integer role) {


        MarketPermissionExample example = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(role);
        List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(example);
        ArrayList<String> perms = new ArrayList<>();
        for (MarketPermission marketPermission : marketPermissions) {
            perms.add(marketPermission.getPermission());
        }
        return perms;
    }

    @Override
    public String queryRoleNameByRoleId(Integer role) {

        String roleName = marketRoleMapper.selectNameById(role);

        return roleName;
    }
}
